<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch">
      <el-form-item label="关键字:" prop="nickName">
        <el-input
          size="mini"
          v-model="queryParams.nickName"
          placeholder="请输入手机号/昵称/姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="性别:" prop="sex">
        <el-select v-model="queryParams.sex" placeholder="请选择性别" clearable size="mini">
          <el-option
            v-for="dict in dict.type.sys_user_sex"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="状态:" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择状态" clearable size="mini">
          <el-option
            v-for="dict in dict.type.wx_user_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="类型标识:" prop="typeMark">
        <el-select v-model="queryParams.typeMark" placeholder="请选择类型标识" clearable size="mini">
          <el-option
            v-for="dict in dict.type.type_mark"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-table v-loading="loading" :data="dataList">
      <el-table-column label="序号" type="index" align="center" min-width="5%"/>
      <el-table-column label="用户昵称" :show-overflow-tooltip="true" prop="nickName" key="nickName" align="center"/>
      <el-table-column label="真实姓名" prop="realName" key="realName" align="center"/>
      <el-table-column label="手机号码" prop="phone" key="phone" align="center"/>
      <el-table-column label="用户头像" key="headImg" align="center">
        <template slot-scope="scope">
          <image-preview class="list-img" :src="scope.row.headImg"/>
        </template>
      </el-table-column>
      <el-table-column label="性别" prop="sex" key="sex" align="center">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_user_sex" :value="scope.row.sex"/>
        </template>
      </el-table-column>
      <el-table-column label="状态" prop="status" key="status" align="center">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.wx_user_status" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column label="类型标识" prop="typeMark" key="typeMark" align="center">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.type_mark" :value="scope.row.typeMark"/>
        </template>
      </el-table-column>
      <el-table-column label="创建时间" prop="createTime" align="center">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" class-name="small-padding fixed-width" align="center">
        <template slot-scope="scope">
          <el-button
            v-if="scope.row.status === '1'"
            size="mini"
            type="text"
            @click="changeStatus(scope.row)"
            v-hasPermi="['system:wxUser:changeStatus']"
          >禁用
          </el-button>
          <el-button
            v-if="scope.row.status === '2'"
            size="mini"
            type="text"
            @click="changeStatus(scope.row)"
            v-hasPermi="['system:wxUser:changeStatus']"
          >启用
          </el-button>
          <el-button
            size="mini"
            type="text"
            @click="pwdReset(scope.row)"
            v-hasPermi="['system:wxUser:pwdReset']"
          >密码重置
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
  </div>
</template>
<script>
import { pageList,pwdReset, changeStatus } from '@/api/triones/wxUser'
export default {
  name: "index",
  dicts: ['sys_user_sex', 'wx_user_status', 'type_mark'],
  data() {
    return {
      showSearch: true,
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        nickName: undefined,
        sex: undefined,
        status: undefined,
        typeMark: undefined
      },
      loading: false,
      total: 0,
      dataList: [],
    }
  },
  created() {
    this.getList();
  },
  methods: {
    getList() {
      this.loading = true;
      pageList(this.queryParams).then(response => {
        this.dataList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    changeStatus(row) {
      let text = row.status === '1' ? "禁用" : "启用";
      let value = row.status === '1' ? "2" : "1";
      this.$modal.confirm('确认要"' + text + '""' + '该账号吗？').then(function() {
        return changeStatus(row.id, value);
      }).then(() => {
        this.$modal.msgSuccess(text + "成功");
        this.getList();
      })
    },
    pwdReset(row) {
      this.$prompt('请输入"' + row.nickName + '"的新密码', "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        closeOnClickModal: false,
        inputPattern: /^.{5,20}$/,
        inputErrorMessage: "用户密码长度必须介于 5 和 20 之间"
      }).then(({value}) => {
        pwdReset(row.id, value).then(response => {
          this.$modal.msgSuccess("修改成功，新密码是：" + value);
        });
      }).catch(() => {
      });
    }
  },
}
</script>
<style scoped lang="scss">

</style>
